﻿using Iworld.Author.Model;
using Iworld.Author.Service.Backstage.Export;
using Iworld.basis.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace Iworld.Author.Service.Backstage.Import
{
    /// <summary>
    /// 用户帐变记录
    /// </summary>
   [DataContract]
    public class GetGoldsChangeRecordImport : GetPageListCheckAdministratorImportBase
    {
        /// <summary>
        /// 开始时间
        /// </summary>
        [DataMember]
        public DateTime? BeginTime { get; set; }

        /// <summary>
        /// 结束时间
        /// </summary>
        [DataMember]
        public DateTime? EndTime { get; set; }

       /// <summary>
       /// 关键字
       /// </summary>
       [DataMember]
       public string Keyword { set; get; }

       /// <summary>
       /// 帐变类型
       /// </summary>
       [DataMember]
       public string Type { set; get; }

        public PageResult<AuthorGoldsChangeRecordExport> GetGoldsChangeRecords(IModelToDbContextOfAuthor db)
        {
           int pageSize = 10;
           int startRow = pageSize * (this.PageIndex - 1);
           IQueryable<GoldChangeRecord> temp = db.GoldChangeRecords;
           if (BeginTime != null)
           {
               temp=temp.Where(x => x.CreatedTime > (DateTime)BeginTime);
           }
           if (EndTime != null)
           {
               temp=temp.Where(x => x.CreatedTime < (DateTime)EndTime);
           }
           if (Keyword != null && Keyword.Trim().Length > 0)
           {
               temp=temp.Where(x => x.Owner.Username.Contains(Keyword)
                   || x.Owner.Account.Contains(Keyword));
           }
            if(Type!=null)
            {
                temp = temp.Where(x=>x.Type == this.Type);
            }
           int totalCount = temp.Count();
           var list = temp.OrderByDescending(x => x.Id).Skip(startRow).Take(pageSize).ToList().ConvertAll(x =>
           {
               return new AuthorGoldsChangeRecordExport(x);
           });

           return new PageResult<AuthorGoldsChangeRecordExport>(this.PageIndex, totalCount, pageSize, list);
       }
    }
}
